<template>
  <div class="app-container">
    <div class="filter-container">
      <SearchOptionUser @setSearchUserId="searchToUserId" />
      <SearchOptionClass :class-id="listQuery.class_id" @setSearchClassId="searchClassId" />
      <el-select v-model="listQuery.is_read" placeholder="是否已读" clearable class="filter-item" style="width: 130px">
        <el-option v-for="(value,key) in isReadOptions" :key="key" :label="value" :value="key" />
      </el-select>
      <el-input v-model="listQuery.content" placeholder="内容" style="width: 100px;" class="filter-item" />
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        查询
      </el-button>
    </div>

    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :header-cell-style="{background:'#ccc',color:'black'}"
      :data="list"
      border
      stripe
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column label="发起人姓名" align="center">
        <template slot-scope="{row}">
          <span>{{ row.fromUserInfo.name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="接收人姓名" align="center">
        <template slot-scope="{row}">
          <span>{{ row.toUserInfo.name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="接收人班级" align="center">
        <template slot-scope="{row}">
          <span>{{ row.toClassInfo.name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="内容" align="center">
        <template slot-scope="{row}">
          <span>{{ row.content }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否已读" align="center">
        <template slot-scope="{row}">
          <span>{{ isReadOptions[row.is_read] }}</span>
        </template>
      </el-table-column>
      <el-table-column label="时间" align="center">
        <template slot-scope="{row}">
          <span>{{ row.created_at }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />

  </div>
</template>

<script>
import { userMessagePage } from '@/api/user'
import waves from '@/directive/waves' // waves directive
import { listQueryFilter } from '@/utils/tool'
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import SearchOptionUser from '@/components/SearchOption/user'
import SearchOptionClass from '@/components/SearchOption/class'

export default {
  name: 'UserMessagePage',
  components: { Pagination, SearchOptionUser, SearchOptionClass },
  directives: { waves },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: { // 查询初始值
        page: 1,
        limit: 20,
        class_id: '',
        to: '',
        is_read: '',
        content: ''
      },
      isReadOptions: {
        0: '未读',
        1: '已读'
      },
      downloadLoading: false
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      this.listQuery = listQueryFilter(this.listQuery)
      userMessagePage(this.listQuery).then(response => {
        this.list = response.data
        this.total = response.page
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    searchToUserId: function(data) {
      this.listQuery.to = data.userId
    },
    searchClassId: function(data) {
      this.listQuery.class_id = data.classId
    }
  }
}
</script>
